<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\GeoHash;
use app\api\model\DidaApi as DidaApiModel;

class DidaApi extends Api
{
    public function index(){

        $BedTypeList = cache("dida_BedTypeList");//床型
        $BreakfastTypeList = cache("dida_BreakfastTypeList");//早餐

        echo "<pre>";
        var_dump($BedTypeList);
        echo "</pre>";
        exit;

    }

    public function GetCountryList()
    {
        $DidaApiModel = new DidaApiModel();
        $data = $DidaApiModel->GetCountryList();
    }
    /*
        调用静态方法获取数据
        https://h.kuaibangxing.com/api/dida_api/getStaticData?func=GetBedTypeList
        床型  GetBedTypeList
        早餐  GetBreakfastTypeList
        餐型  GetMealTypeList
        酒店类别  GetPropertyCategoryList
    */
    public function getStaticData(){
        $get = $this->request->get();
        $funcName = $get['func'];
        $DidaApiModel = new DidaApiModel();
        $data = $DidaApiModel->{$funcName}();
        return $this->result('success',$data,200,'json');
    }

    /*
        缓存静态数据      dida_
        床型   早餐  餐型   酒店类别
        BedType      BreakfastType      MealType    PropertyCategory
    */
    public function saveDidaCache(){
        $DidaApiModel = new DidaApiModel();
        $funcArr = ['BedType','BreakfastType','MealType','PropertyCategory'];
        foreach ($funcArr as $funcName) {
            $cacheData = [];
            $data = $DidaApiModel->{"Get".$funcName."List"}();
            if(!empty($data['Success'])){
                $cacheData = $data['Success'];
            }
            cache("dida_".$funcName."List",$cacheData);
        }
    }

    public function getCity($code){
        $DidaApiModel = new DidaApiModel();
        $cityDataArr = $DidaApiModel->GetCityList($code);
        return $this->result('success',$cityDataArr,200,'json');
    }

    public function saveCity(){
        ignore_user_abort();//关闭浏览器仍然执行
        set_time_limit(0);//让程序一直执行下去

        $DidaCityData = db("didacity")->field("CityCode")->select();
        $DidaCityArr = array_column($DidaCityData,"CityCode");

        $DidaApiModel = new DidaApiModel();
        $cityCodeArr = ['US','CA'];
        $newCityArr = [];
        foreach ($cityCodeArr as $key=>$value) {
            $cityDataArr = $DidaApiModel->GetCityList($value);
            foreach ($cityDataArr as $k=>$v) {
                if(!in_array($v['CityCode'],$DidaCityArr)){
                    $newCityArr[] = [
                        'CityCode'=>$v['CityCode'],
                        'CityName'=>$v['CityName'],
                        'CityName_CN'=>!empty($v['CityName_CN'])?$v['CityName_CN']:'',
                        'CityLongName'=>$v['CityLongName'],
                        'CityLongName_CN'=>!empty($v['CityLongName_CN'])?$v['CityLongName_CN']:'',
                        'CountryCode'=>$v['CountryCode'],
                        'Type'=>$v['Type'],
                        'ParentCityCode'=>!empty($v['ParentCityCode'])?$v['ParentCityCode']:'',
                    ];
                }
            }
        }

        $insertRes = db("didacity")->insertAll($newCityArr);

        var_dump($insertRes);
        exit;
    }
    //获取didaHotel详情【静态数据】
    public function getDidaHotelDetails($page=1,$offset=20){
        $DidaApiModel = new DidaApiModel();
        $listUrl = $DidaApiModel->GetStaticInformation();
        $data = file_get_contents($listUrl);

        $data = explode("\r\n",$data);
        $field = explode("|",mb_substr($data[0],1));
        unset($data[0]);
        $hotelArr = [];
        //data的下标从1开始
        foreach ($data as $key=>$value) {
            if(!empty($value) && $key>($page-1)*$offset && $key<=$page*$offset) $hotelArr[] = array_combine($field,explode("|",$value));
        }
        return $this->result('success',$hotelArr,200,'json');
    }

    public function saveHotel(){
        ignore_user_abort();//关闭浏览器仍然执行
        set_time_limit(0);//让程序一直执行下去

        $DidaApiModel = new DidaApiModel();
        $listUrl = $DidaApiModel->GetStaticInformation();
        $data = file_get_contents($listUrl);
        if(empty($data)){
            echo "无数据";
            exit;
        }

        $fieldArr = ["HotelID","Name","Name_CN","Address","CityCode","CityName","CityName_CN","StateCode","CountryCode","CountryName","CountryName_CN","ZipCode","Longitude","Latitude","StarRating","Telephone","AirportCode","PropertyCategory","DestinationID","DestinationName","DestinationName_CN","Address_CN","UpdateDate"];

        $hotelArr = [];

        foreach ($data as $key=>$value) {
            if($key==0){
                continue;
            }
            if(strstr($value,"美国")==false && strstr($value,"加拿大")==false){
                continue;
            }

            $hotelData0 = explode("|",$value);
            $hotelData = [];
            foreach ($hotelData0 as $k=>$v) {
                $hotelData[$fieldArr[$k]] = $v;
            }

            if(empty($hotelData['HotelID'])){
                continue;
            }

            $hotelArr[] = [
                'HotelID'=>$hotelData['HotelID'],
                'Name'=>$hotelData['Name'],
                'Name_CN'=>$hotelData['Name_CN'],
                'Address'=>$hotelData['Address'],
                'CityCode'=>$hotelData['CityCode'],
                'CityName'=>$hotelData['CityName'],
                'CityName_CN'=>!empty($hotelData['CityName_CN'])?$hotelData['CityName_CN']:'',
                'StateCode'=>$hotelData['StateCode'],
                'CountryCode'=>$hotelData['CountryCode'],
                '﻿CountryName'=>!empty($hotelData['﻿CountryName'])?$hotelData['﻿CountryName']:'',
                'CountryName_CN'=>!empty($hotelData['CountryName_CN'])?$hotelData['CountryName_CN']:'',
                'ZipCode'=>$hotelData['ZipCode'],
                'Longitude'=>$hotelData['Longitude'],
                'Latitude'=>$hotelData['Latitude'],
                'StarRating'=>$hotelData['StarRating'],
                'Telephone'=>$hotelData['Telephone'],
                'AirportCode'=>$hotelData['AirportCode'],
                'PropertyCategory'=>$hotelData['PropertyCategory'],
                'DestinationID'=>$hotelData['DestinationID'],
                'DestinationName'=>$hotelData['DestinationName'],
                'DestinationName_CN'=>$hotelData['DestinationName_CN'],
                'Address_CN'=>$hotelData['Address_CN'],
                'UpdateDate'=>$hotelData['UpdateDate'],
            ];
        }

        $insertRes = db("didahotel")->insertAll($hotelArr,true);

        var_dump($insertRes);
        exit;
    }

    public function getCacheLowerPrice(){
        ignore_user_abort();//关闭浏览器仍然执行
        set_time_limit(0);//让程序一直执行下去

        $hotelArr = db("didahotel")->field("HotelID")->limit(50)->select();
        $HotelIDArr = array_column($hotelArr,"HotelID");

        $CheckInDate = "2020-10-01";
        $CheckOutDate = "2020-10-29";

        $DidaApiModel = new DidaApiModel();
        $lowerPriceData = $DidaApiModel->priceSearch($HotelIDArr,$CheckInDate,$CheckOutDate);
        return $this->result('success',$lowerPriceData,200,'json');
        echo "<pre>";
        var_dump($lowerPriceData);
        echo "</pre>";
        exit;



    }

    public function getLivePrice($data){
        $CheckInDate = $data['CheckInDate'];
        $CheckOutDate = $data['CheckOutDate'];
        $HotelID = $data['HotelID'];
        $RoomCount = $data['RoomCount'];
        $AdultCount = $data['AdultCount'];
        $ChildCount = $data['ChildCount'];
        $ChildAgeDetails = $data['ChildAgeDetails'];

        $DidaApiModel = new DidaApiModel();
        $PriceData = $DidaApiModel->realTimePrice($CheckInDate,$CheckOutDate,$HotelID,$RoomCount,$AdultCount,$ChildCount,$ChildAgeDetails);
        return $PriceData;
//        return $this->result('success',$PriceData,200,'json');
    }

    public function getLivePriceTest(){
        $CheckInDate = '2020-10-01';
        $CheckOutDate = '2020-10-04';
        $HotelID = 100070;
        $RoomCount = 1;
        $AdultCount = 1;
        $ChildCount = 0;
        $ChildAgeDetails = [];

        $DidaApiModel = new DidaApiModel();
        $PriceData = $DidaApiModel->realTimePrice($CheckInDate,$CheckOutDate,$HotelID,$RoomCount,$AdultCount,$ChildCount,$ChildAgeDetails);
        return $this->result('success',$PriceData,200,'json');
    }


}